﻿namespace GuaHao
{
    public partial class FormPatient : Form
    {
        string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data", "data_patient.txt");
        //患者表
        private List<Patients> patients = new List<Patients>();
        public FormPatient()
        {
            InitializeComponent();
            InitializeDataGridView();
            TwodateTimePicker.Value = DateTime.Now;
            OnedateTimePicker.Value = DateTime.Now.AddDays(-1);
            OnedateTimePicker.ValueChanged += OnDateTimePickerValueChanged;
            TwodateTimePicker.ValueChanged += OnDateTimePickerValueChanged;
            Nametextbox.TextChanged += OnTextBoxTextChanged;
            Sfztextbox.TextChanged += OnTextBoxTextChanged;
        }
        //初始化表格
        private void InitializeDataGridView()
        {
            //// 添加编辑按钮列
            //var editButtonColumn = new DataGridViewButtonColumn
            //{
            //    Name = "操作",
            //    HeaderText = "操作",
            //    Text = "编辑",
            //    UseColumnTextForButtonValue = true
            //};
            //dataGridViewPatients.Columns.Add(editButtonColumn);

            //// 将“操作”列放到最后
            //editButtonColumn.DisplayIndex = dataGridViewPatients.Columns.Count - 1;

            // 绑定数据
            RefreshDataGridView();
        }

        // 刷新 DataGridView
        private void RefreshDataGridView()
        {
            DateTime selectedDateTimeBegin = OnedateTimePicker.Value;
            DateTime selectedDateTimeEnd = TwodateTimePicker.Value;
            //检查是否存在姓名或身份证的过滤条件
            bool hasNameFilter = !string.IsNullOrWhiteSpace(Nametextbox.Text);
            bool hasSfzFilter = !string.IsNullOrWhiteSpace(Sfztextbox.Text);
            patients = TextJson.ReadPatients(path);
            if (selectedDateTimeEnd >= selectedDateTimeBegin)
            {
                if (hasNameFilter || hasSfzFilter)
                {
                    if (hasNameFilter)
                    {
                        string nameKeyword = Nametextbox.Text.Trim();
                        patients = patients.Where(p => p.Name.Contains(nameKeyword) && p.CreateTime >= selectedDateTimeBegin && p.CreateTime <= selectedDateTimeEnd).ToList();
                    }
                    if (hasSfzFilter)
                    {
                        string sfzKeyword = Sfztextbox.Text.Trim();
                        patients = patients.Where(p => p.IDCard.Equals(sfzKeyword, StringComparison.OrdinalIgnoreCase) && p.CreateTime >= selectedDateTimeBegin && p.CreateTime <= selectedDateTimeEnd).ToList();
                    }
                }
                else
                {
                    patients = patients.Where(p => p.CreateTime >= selectedDateTimeBegin && p.CreateTime <= selectedDateTimeEnd).ToList();
                }
                dataGridViewPatients.DataSource = null;
                dataGridViewPatients.DataSource = patients;
            }
            else
            {
                MessageBox.Show("开始时间不能大于结束时间", "提示", MessageBoxButtons.OK);
            }
        }
        // 当 DateTimePicker 的内容发生改变时调用此方法
        private void OnDateTimePickerValueChanged(object sender, EventArgs e)
        {
            // 调用刷新 DataGridView 的方法
            RefreshDataGridView();

        }

        // 当 TextBox 的文本发生改变时调用此方法
        private void OnTextBoxTextChanged(object sender, EventArgs e)
        {
            // 调用刷新 DataGridView 的方法
            RefreshDataGridView();
        }

        //打开录入界面
        private void BtnAddPatient_Click(object sender, EventArgs e)
        {
            using (var addPatientForm = new AddPatientForm())
            {
                if (addPatientForm.ShowDialog() == DialogResult.OK)
                {
                    patients.Add(addPatientForm.Patient);
                    RefreshDataGridView();
                }
            }
        }
        //修改点击事件
        private void BtnRevise_Click(object sender, EventArgs e)
        {
            try
            {
                // 检查是否有选中的行
                if (dataGridViewPatients.SelectedRows.Count > 0)
                {
                    // 获取选中的行索引
                    int selectedRowIndex = dataGridViewPatients.SelectedRows[0].Index;

                    // 获取选中的患者
                    var selectedPatient = patients[selectedRowIndex];

                    // 打开编辑窗口
                    using (var editPatientForm = new AddPatientForm(selectedPatient))
                    {
                        if (editPatientForm.ShowDialog() == DialogResult.OK)
                        {
                            // 更新患者信息
                            patients[selectedRowIndex] = editPatientForm.Patient;
                            RefreshDataGridView();

                            // 保存更新后的数据到 JSON 文件
                            TextJson.WritePatients(patients, path);
                        }
                    }
                }
                else
                {
                    MessageBox.Show("请先选择一行数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"操作失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        ////表格中的编辑列
        //private void dataGridViewPatients_CellContentClick(object sender, DataGridViewCellEventArgs e)
        //{
        //    if (e.RowIndex >= 0 && e.ColumnIndex == dataGridViewPatients.Columns["操作"].Index)
        //    {
        //        // 获取选中的患者
        //        var selectedPatient = patients[e.RowIndex];

        //        // 打开编辑窗口
        //        using (var editPatientForm = new AddPatientForm(selectedPatient))
        //        {
        //            if (editPatientForm.ShowDialog() == DialogResult.OK)
        //            {
        //                // 更新患者信息
        //                patients[e.RowIndex] = editPatientForm.Patient;
        //                RefreshDataGridView();
        //            }
        //        }
        //    }
        //}

    }
}
